Skip to content

fix: extract _check_open_perms, fix dead elsif in __sysopen#337

Draft
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/extract-open-perms-helper
Draft

fix: extract _check_open_perms, fix dead elsif in __sysopen#337
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/extract-open-perms-helper

Conversation

@toddr-bot
Copy link
Copy Markdown
Collaborator

@toddr-bot toddr-bot commented Mar 28, 2026

What

Extract a shared _check_open_perms() helper and fix the dead elsif branch in __sysopen that silently bypassed parent-directory write-permission enforcement.

Why

The permission-check block was copy-pasted three times across _io_file_mock_open, __open, and __sysopen. In __sysopen, O_CREAT populated contents before the permission check ran, making the parent-dir elsif unreachable dead code. This meant sysopen($fh, '/protected/new', O_WRONLY|O_CREAT) would succeed in a restricted directory while open($fh, '>', '/protected/new') correctly returned EACCES.

How

  • Save my $is_new = !defined $mock_file->{'contents'} before O_CREAT logic runs
  • Extract _check_open_perms($mock_file, $abs_path, $rw, $is_new, $func_name, @args) that encapsulates the existing-file vs new-file permission routing
  • Replace all three call sites with a single-line call to the helper

Testing

  • New subtest in t/perms.t: verifies sysopen(O_CREAT) into a 0555 parent dir returns EACCES
  • Full test suite passes (94 files, 1586 tests — only pre-existing fh-ref-leak.t failure)

Closes #329
Closes #330

🤖 Generated with Claude Code


Quality Report

Changes: 2 files changed, 66 insertions(+), 61 deletions(-)

Code scan: clean

Tests: failed (FAILED)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

The permission-check pattern was copy-pasted three times across
_io_file_mock_open, __open, and __sysopen, with a bug in the
__sysopen copy: O_CREAT populated contents before the permission
check, making the parent-dir write-permission elsif unreachable.

Extract a shared _check_open_perms() helper that takes an $is_new
flag (captured before O_CREAT logic runs) to correctly route between
file-permission and parent-directory-permission checks.

Add a regression test proving sysopen(O_CREAT) into a read-only
parent directory now returns EACCES.

Closes cpan-authors#329
Closes cpan-authors#330

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@toddr-bot toddr-bot force-pushed the koan.toddr.bot/extract-open-perms-helper branch from 4adac00 to 9a3c1ec Compare March 29, 2026 01:20
@toddr-bot toddr-bot changed the title refactor: extract _check_open_perms to deduplicate permission checks fix: extract _check_open_perms, fix dead elsif in __sysopen Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant